package com.hnchances.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnchances.common.vo.ResultVo;
import com.hnchances.entity.Class;
import com.hnchances.entity.Course;
import com.hnchances.entity.Exam;
import com.hnchances.entity.Grades;
import com.hnchances.entity.vo.GradeVo;
import com.hnchances.mapper.ClassMapper;
import com.hnchances.mapper.CourseMapper;
import com.hnchances.mapper.ExamMapper;
import com.hnchances.mapper.GradesMapper;
import com.hnchances.service.GradesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-08-11
 */
@Service
public class GradesServiceImpl extends ServiceImpl<GradesMapper, Grades> implements GradesService {
    @Autowired
    private GradesMapper gradesMapper;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ExamMapper examMapper;

    @Override
    public ResultVo add(Grades grades) {
        int i = gradesMapper.insert(grades);
        if (i > 0){
            return ResultVo.ok();
        }else {
            return ResultVo.error();
        }
    }

    @Override
    public ResultVo delete(BigInteger gradeId) {
        int i = gradesMapper.deleteById(gradeId);
        if (i > 0){
            return ResultVo.ok();
        }else {
            return ResultVo.error();
        }
    }

    @Override
    public ResultVo select(GradeVo gradevo, Integer pageNum, Integer pageSize) {
        Page<Grades> gradesPage = new Page<>(pageNum, pageSize);
        // 获取课程ID
        String className = gradevo.getClassName();
        QueryWrapper<Class> classQueryWrapper = new QueryWrapper<>();
        classQueryWrapper.eq("className",className);
        Class aClass = classMapper.selectOne(classQueryWrapper);
        Integer classId = aClass.getId();

        // 获取班级ID
        String courseName = gradevo.getCourseName();
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq("courseName",courseName);
        Course course = courseMapper.selectOne(courseQueryWrapper);
        Integer courseId = course.getId();

        // 获取考试ID
        String examName = gradevo.getExamName();
        QueryWrapper<Exam> examQueryWrapper = new QueryWrapper<>();
        examQueryWrapper.eq("examName", examName);
        Exam exam = examMapper.selectOne(examQueryWrapper);
        Integer examId = exam.getId();

        QueryWrapper<Grades> wrapper = new QueryWrapper<>();
        wrapper.eq("courseID",courseId)
                .eq("exmID",examId)
                .eq("classID",classId);

        Page<Grades> page = gradesMapper.selectPage(gradesPage, wrapper);
        return ResultVo.ok().data("page",page);
    }

    @Override
    public ResultVo updateGrade(Grades grades) {
        int i = gradesMapper.updateById(grades);
        if (i > 0){
            return ResultVo.ok();
        }else {
            return ResultVo.error();
        }
    }
}
